package com.suning.sawp.member.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.suning.sawp.member.constant.MemberConstants;
import com.suning.sawp.member.dao.StoreExtensionDao;
import com.suning.sawp.member.vo.MemberExtensionCateVo;
import com.suning.sawp.member.vo.MemberExtensionClientVo;
import com.suning.sawp.member.vo.MemberExtensionEmpVo;
import com.suning.sawp.member.vo.MemberExtensionStatisVo;
import com.suning.sawp.service.util.DateUtils;

/**
 * 
 * 会员拓展dao实现 <br> 
 *
 * @author 14060886 祝翔龙
 */
@Repository
public class StoreExtensionDaoImpl extends BaseDaoImpl implements StoreExtensionDao{
    
    @Override
    public MemberExtensionStatisVo selectExpandStatisInfoByStore(String activityId, String storeCode, String dateType, Short source) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("source", source);
        if(dateType.equals(MemberConstants.EXPAND_TODAY)){
            params.put("createTime", new Date());
        }else if(dateType.equals(MemberConstants.INQUIRE_YESTERDAY) && source == MemberConstants.INQUIRY_NOT_BUY){
            params.put("createTime", DateUtils.getNextDay(new Date(), -1));
        }else if(source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_MONTH)){
            params.put("startTime", DateUtils.getFirstDayZeroPointOfMonth(0));
            params.put("endTime", DateUtils.getFirstDayZeroPointOfMonth(1));
        }
        return dalClientRead.queryForObject("memberExtension.selectExpandStatisInfoByStore", params, MemberExtensionStatisVo.class);
    }

    @Override
    public List<MemberExtensionCateVo> selectExpandCateInfoByStore(String activityId, String storeCode, String dateType, Short source) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("dateType", dateType);
        params.put("source", source);
        if(dateType.equals(MemberConstants.EXPAND_TODAY)){
            params.put("createTime", new Date());
        }else if(source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_YESTERDAY)){
            params.put("createTime", DateUtils.getNextDay(new Date(), -1));
        }else if(source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_MONTH)){
            params.put("startTime", DateUtils.getFirstDayZeroPointOfMonth(0));
            params.put("endTime", DateUtils.getFirstDayZeroPointOfMonth(1));
        }
        if(source.equals(MemberConstants.STORE_EXPEND)){
            return dalClientRead.queryForList("memberExtension.selectExpandCateInfoByStore", params, MemberExtensionCateVo.class);
        }else{
            return dalClientRead.queryForList("memberExtension.selectInquiryCateInfoByStore", params, MemberExtensionCateVo.class);
        }
    }

    @Override
    public Integer selectTargetPeopleByStore(String activityId, String storeCode, String cateCode) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("cateCode", cateCode);
        return dalClientRead.queryForObject("memberExtension.selectTargetPeopleByStore", params, Integer.class);
    }
    

    @Override
    public MemberExtensionStatisVo selectExpandStatisInfoByCate(String activityId, String storeCode, String cateCode, String dateType, Short source) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("cateCode", cateCode);
        params.put("source", source);
        if(source == MemberConstants.INQUIRY_NOT_BUY && dateType.equals(MemberConstants.INQUIRE_TODAY)){
            params.put("createTime", new Date());
        }else if( source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_YESTERDAY)){
            params.put("createTime", DateUtils.getNextDay(new Date(), -1));
        }else if(source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_MONTH)){
            params.put("startTime", DateUtils.getFirstDayZeroPointOfMonth(0));
            params.put("endTime", DateUtils.getFirstDayZeroPointOfMonth(1));
        }
        return dalClientRead.queryForObject("memberExtension.selectExpandStatisInfoByCate", params, MemberExtensionStatisVo.class);
    }

    @Override
    public List<MemberExtensionEmpVo> selectExpandEmpInfoByCate(String activityId, String storeCode, String cateCode, String dateType, Short source) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("cateCode", cateCode);
        params.put("dateType", dateType);
        params.put("source", source);
        if(source == MemberConstants.INQUIRY_NOT_BUY && dateType.equals(MemberConstants.INQUIRE_TODAY)){
            params.put("createTime", new Date());
        }else if( source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_YESTERDAY)){
            params.put("createTime", DateUtils.getNextDay(new Date(), -1));
        }else if(source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_MONTH)){
            params.put("startTime", DateUtils.getFirstDayZeroPointOfMonth(0));
            params.put("endTime", DateUtils.getFirstDayZeroPointOfMonth(1));
        }
        if(source.equals(MemberConstants.STORE_EXPEND)){
            return dalClientRead.queryForList("memberExtension.selectExpandEmpInfoByCate", params, MemberExtensionEmpVo.class);
        }else{
            return dalClientRead.queryForList("memberExtension.selectInquiryEmpInfoByCate", params, MemberExtensionEmpVo.class);
        }
    }

    @Override
    public MemberExtensionStatisVo selectExpandStatisInfoByLoc(String activityId, String storeCode, String expandId, Short source) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("expandId", expandId);
        params.put("source", source);
        return dalClientRead.queryForObject("memberExtension.selectExpandStatisInfoByLoc", params, MemberExtensionStatisVo.class);
    }

    @Override
    public List<MemberExtensionEmpVo> selectExpandEmpInfoByLoc(String activityId, String storeCode, String expandId, String dateType, Short source) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("expandId", expandId);
        params.put("dateType", dateType);
        params.put("source", source);
        return dalClientRead.queryForList("memberExtension.selectExpandEmpInfoByLoc", params, MemberExtensionEmpVo.class);
    }

    @Override
    public Integer selectTargetPeopleByLoc(String expandId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("expandId", expandId);
        return dalClientRead.queryForObject("memberExtension.selectTargetPeopleByLoc", params, Integer.class);
    }

    @Override
    public MemberExtensionStatisVo selectExpandStatisInfoByEmp(String activityId, String storeCode, String employeeId, 
            String expandId, Short source, String dateType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("employeeId", employeeId);
        params.put("expandId", expandId);
        params.put("source", source);
        if(source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_TODAY)){
            params.put("createTime", new Date());
        }else if( source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_YESTERDAY)){
            params.put("createTime", DateUtils.getNextDay(new Date(), -1));
        }else if(source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_MONTH)){
            params.put("startTime", DateUtils.getFirstDayZeroPointOfMonth(0));
            params.put("endTime", DateUtils.getFirstDayZeroPointOfMonth(1));
        }
        return dalClientRead.queryForObject("memberExtension.selectExpandStatisInfoByEmp", params, MemberExtensionStatisVo.class);
    }

    @Override
    public List<MemberExtensionClientVo> selectExpandClientInfoByEmp(String activityId, String storeCode,
            String employeeId, String expandId, Short source, String dateType, Integer start, Integer end) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("employeeId", employeeId);
        params.put("expandId", expandId);
        params.put("source", source);
        params.put("start", start);
        params.put("end", end);
        if(source == MemberConstants.INQUIRY_NOT_BUY && dateType.equals(MemberConstants.INQUIRE_TODAY)){
            params.put("createTime", new Date());
        }else if(source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_YESTERDAY)){
            params.put("createTime", DateUtils.getNextDay(new Date(), -1));
        }else if(source.shortValue() == MemberConstants.INQUIRY_NOT_BUY.shortValue() && dateType.equals(MemberConstants.INQUIRE_MONTH)){
            params.put("startTime", DateUtils.getFirstDayZeroPointOfMonth(0));
            params.put("endTime", DateUtils.getFirstDayZeroPointOfMonth(1));
        }
        return dalClientRead.queryForList("memberExtension.selectExpandClientInfoByEmp", params, MemberExtensionClientVo.class);
    }

    @Override
    public Integer selectTargetPeopleByEmp(String activityId, String storeCode, String employeeId, String expandId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("storeCode", storeCode);
        params.put("employeeId", employeeId);
        if(StringUtils.isNotEmpty(expandId)){
            params.put("expandId", expandId);
        }
        return dalClientRead.queryForObject("memberExtension.selectTargetPeopleByEmp", params, Integer.class);
    }

    @Override
    public List<MemberExtensionClientVo> selectInquiryDetailByEmp4DY(String storeCode, String employeeId,
            Date startDate, Date endDate, Integer start, Integer end) {
        Map<String, Object> params = new HashMap<String, Object>();
      params.put("storeCode", storeCode);
      params.put("employeeId", employeeId);
      params.put("startDate", startDate);
      params.put("endDate", endDate);
      params.put("start", start);
      params.put("end", end);
      return dalClientRead.queryForList("memberExtension.selectInquiryDetailByEmp4DY", params, MemberExtensionClientVo.class);
    }
    
}
